layui.use(['jquery','layer','table','form'],function () {
    var $ = layui.jquery;
    var table = layui.table;
    var form = layui.form;

    var couponName = '';
    var give = table.render({
        elem:"#table",
        url:"/newCoupon/getAllCoupon?couponName="+couponName,
        toolbar:"#tool",
        page:true,
        limit:6,
        limits:[6,12,18,24],
        cols:[
            [
                {type:"checkbox",align:"center"},
                {type:"numbers",title:"序号",align:"center"},
                {field:"couponName",title:"优惠券名称",align:"center"},
                {field:"couponPrice",title:"优惠券面值",align:"center",templet:function (data) {
                        return "￥"+data.couponPrice;
                    }},
                {field:"type",title:"优惠券类型",align:"center",templet:function (data) {
                        if (data.type == 2)
                            return "商品";
                        return '视频';
                    }},
                {field:"",title:"优惠卷有效期",align:"center",width:200,templet:function (data) {
                        return "开始:"+data.startDate
                    }},
                {field:"",title:"优惠卷有效期",align:"center",width:200,templet:function (data) {
                        return "结束："+data.endDate;
                    }},
            ]
        ],
    });
    /*模糊查询*/
    form.on("submit(sub)",function () {
        var couponName = $("#couponName").val();
        give.reload({
            url:"/newCoupon/getCouponGive?couponName="+couponName,
        });
        return false;
    });
    /*确定*/
    $("#queding").click(function () {
        var checkStatus = table.checkStatus("table").data;
        var flag = true;
        if(checkStatus.length > 0){
            checkStatus.forEach(function (data) {
                var getDate = getTime(new Date());
                var userId = $("#userId").val();
                var json = {
                    userId:userId,
                    couponId:data.couponId,
                    getDate:getDate,
                    remarks:2
                };
                $.ajax({
                    url:"/newCoupon/giveCoupon",
                    data:json,
                    dataType:"json",
                    Type:"post",
                    success:function (data) {
                        if (data.data){
                            var index  = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            parent.location.reload();
                        }else{
                            flag = false;
                        }
                    },
                })
            })
        }
        if(flag){
            alert("赠送成功");
        }else{
            alert("赠送失败");
        }
    });
    /*获取当前时间*/
    function getTime(date){
        var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var today = date.getDate(); //获取当前日(1-31)
        var hour = date.getHours(); //获取当前小时数(0-23)
        var minute = date.getMinutes(); //获取当前分钟数(0-59)
        var second = date.getSeconds(); //获取当前秒数(0-59)
        var nowTime;
        nowTime = year+'-'+fillZero(month)+'-'+fillZero(today)+' '+fillZero(hour)+':'+fillZero(minute)+':'+fillZero(second);
        return nowTime;
    };
    /*时间补零*/
    function fillZero(str){
        var realNum;
        if(str<10){
            realNum	= '0'+str;
        }else{
            realNum	= str;
        }
        return realNum;
    };
    /*取消*/
    $("#quxiao").click(function () {
        var index  = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    });
});